import { BaseResponse } from '@/api';
import { getRoleItemSearch, getRoleList, updateStatus } from '@/domain/authority/api/role';
import { OpButtonKey, OpGroupCallBack, OpType, SelectRow } from '@/domain/common/types/page';
import { ReqRoleParam, ResRole, ResRoleSearch, RoleId, RoleInfo } from '@/domain/authority/types/role';
import { notification } from 'ant-design-vue';
import { onMounted, reactive, ref, Ref, toRaw } from 'vue';

export type UseRoleManage = () => {
  rolelist: Ref<Partial<ResRole>>;
  searchList: Ref<ResRoleSearch>;
  changeRoleState: (type: OpType, status: boolean) => void;
  opGroupCallBack: OpGroupCallBack<RoleInfo>;
  activeId: Ref<number>;
  addRoleStatus: Ref<boolean>;
  updateRoleStatus: Ref<boolean>;
  showRoleStatus: Ref<boolean>;
  setSizeChange: (current: number, pageSize: number) => Promise<void>;
  selectRoleInfo: Ref<Partial<RoleInfo>>;
  loadTable: (param: Partial<ReqRoleParam>) => Promise<BaseResponse<ResRole>>;
  selectRows: Ref<SelectRow>;
  getSelectRowsCallBack: (selectKey: Array<RoleId>, selectRow: Array<RoleInfo>) => void;
  reloadList: () => void;
};
/**
 *
 * @returns 角色增删改查hooks
 */
export const useRoleManage: UseRoleManage = () => {
  const searchList: Ref<ResRoleSearch> = ref([]);
  const rolelist: Ref<Partial<ResRole>> = ref({});
  const searchParam: Partial<ReqRoleParam> = reactive({ page: 1, pageSize: 10 });
  //列表选择
  const selectRows: Ref<SelectRow> = ref({});

  const getSelectRowsCallBack = (selectKey: Array<RoleId>, selectRow: Array<RoleInfo>) => {
    selectRows.value = { selectKey, selectRow };
  };

  //弹窗状态
  const addRoleStatus = ref(false);
  const updateRoleStatus = ref(false);
  const showRoleStatus = ref(false);
  const selectRoleInfo: Ref<Partial<RoleInfo>> = ref({});
  const activeId = ref(-1);
  //加载table
  const loadTable = async (param: Partial<ReqRoleParam>) => {
    Object.keys(param).forEach((key) => {
      searchParam[key as keyof ReqRoleParam] = param[key as keyof ReqRoleParam];
    });

    return await getRoleList({ ...searchParam });
  };
  //分页状态
  const setSizeChange = async (current: number, pageSize: number) => {
    searchParam.page = current;
    searchParam.pageSize = pageSize;
    rolelist.value = (await loadTable(toRaw(searchParam))).data;
  };
  //页面操作回掉
  const opGroupCallBack: OpGroupCallBack<RoleInfo> = async (key, node) => {
    activeId.value = node.role_id!;
    selectRoleInfo.value = node;

    switch (key) {
      case OpButtonKey.memberAdd:
        addRoleStatus.value = true;
        break;
      case OpButtonKey.memberEdit:
        updateRoleStatus.value = true;
        break;
      case OpButtonKey.memberShow:
        // selectRoleInfo.value=
        showRoleStatus.value = true;
        break;
      case OpButtonKey.memberStatusOff:
        const res = await updateStatus({
          role_id: [node.role_id!],
          status: 2
        });
        if (res.code == 0) {
          notification.success({ message: '冻结成功' });
          await reloadList();
        }
        break;
      case OpButtonKey.memberStatusOn:
        const reson = await updateStatus({
          role_id: [node.role_id!],
          status: 1
        });
        if (reson.code == 0) {
          notification.success({ message: '解冻成功' });
          await reloadList();
        }
        break;
      default:
        notification.warning({ message: '表单按钮回掉未命中' });
    }
  };
  const changeRoleState = (type: OpType, status: boolean) => {
    switch (type) {
      case OpType.add:
        addRoleStatus.value = status;
        break;
      case OpType.edit:
        updateRoleStatus.value = status;
        break;
      case OpType.show:
        showRoleStatus.value = status;
        break;
      default:
        console.log({ message: '修改菜单状态未命中' });
    }
  };

  const reloadList = async () => {
    rolelist.value = (await loadTable(toRaw(searchParam))).data;
  };

  onMounted(async () => {
    searchList.value = (await getRoleItemSearch()).data;
    rolelist.value = (await loadTable(toRaw(searchParam))).data;
  });

  return {
    rolelist,
    searchList,
    changeRoleState,
    opGroupCallBack,
    activeId,
    addRoleStatus,
    updateRoleStatus,
    setSizeChange,
    loadTable,
    showRoleStatus,
    selectRoleInfo,
    selectRows,
    getSelectRowsCallBack,
    reloadList
  };
};
